package graphics.tools;

import graphics.shapes.Rod;

import javax.vecmath.Vector3d;

/**
 * Calculating position of new GeoMag spheres placed on a GeoMag rod.
 * 
 * @author: Eirik Vigeland
 * @version: %I%, %G%
 */
public class BallPosition extends Vector3d {

	private static final long serialVersionUID = 1L;

	/**
	 * Constructor setting the properties for the Vector3d to be created as
	 * coordinates.
	 * 
	 * @param rod
	 *            An instant of the rod the ball is attached to.
	 */
	public BallPosition(Rod rod) {
		rod.getTransform3D().get(this);
		double displacement, angularOffset = rod.getAngle()
				- ShapeData.PERPENDICULAR, angle = rod.getAngle();

		// PLACE ON TOP ROD
		if (rod.getPositionOfRod() == ShapeData.TOP) {
			if (rod.getRotation() == ShapeData.ROTATE_X) {
				displacement = getY()
						- ShapeData.angleOffsetBall(angularOffset, ShapeData.SIN);
				setY(displacement);
				displacement = getZ()
						+ ShapeData.angleOffsetBall(angularOffset, ShapeData.COS);
				setZ(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Y
					|| rod.getRotation() == ShapeData.ROTATE_DISABLE) {
				displacement = getY() + ShapeData.C_OFFSET;
				setY(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Z) {
				displacement = getX()
						- ShapeData.angleOffsetBall(angle, ShapeData.SIN);
				setX(displacement);
				displacement = getY()
						+ ShapeData.angleOffsetBall(angle, ShapeData.COS);
				setY(displacement);
			}
		}

		// PLACE ON BOTTOM ROD
		else if (rod.getPositionOfRod() == ShapeData.BOTTOM) {
			if (rod.getRotation() == ShapeData.ROTATE_X) {
				displacement = getY()
						+ ShapeData.angleOffsetBall(angularOffset, ShapeData.SIN);
				setY(displacement);
				displacement = getZ()
						- ShapeData.angleOffsetBall(angularOffset, ShapeData.COS);
				setZ(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Y
					|| rod.getRotation() == ShapeData.ROTATE_DISABLE) {
				displacement = getY() - ShapeData.C_OFFSET;
				setY(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Z) {
				displacement = getX()
						+ ShapeData.angleOffsetBall(angle, ShapeData.SIN);
				setX(displacement);
				displacement = getY()
						- ShapeData.angleOffsetBall(angle, ShapeData.COS);
				setY(displacement);
			}
		}
		// PLACE ON RIGHT ROD
		else if (rod.getPositionOfRod() == ShapeData.RIGHT) {
			if (rod.getRotation() == ShapeData.ROTATE_X
					|| rod.getRotation() == ShapeData.ROTATE_DISABLE) {
				displacement = getX() + ShapeData.C_OFFSET;
				setX(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Y) {
				displacement = getX()
						+ ShapeData.angleOffsetBall(angle, ShapeData.COS);
				setX(displacement);
				displacement = getZ()
						- ShapeData.angleOffsetBall(angle, ShapeData.SIN);
				setZ(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Z) {
				displacement = getX()
						- ShapeData.angleOffsetBall(angularOffset, ShapeData.SIN);
				setX(displacement);
				displacement = getY()
						+ ShapeData.angleOffsetBall(angularOffset, ShapeData.COS);
				setY(displacement);
			}
		}
		// PLACE ON LEFT ROD
		else if (rod.getPositionOfRod() == ShapeData.LEFT) {
			if (rod.getRotation() == ShapeData.ROTATE_X
					|| rod.getRotation() == ShapeData.ROTATE_DISABLE) {
				displacement = getX() - ShapeData.C_OFFSET;
				setX(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Y) {
				displacement = getX()
						- ShapeData.angleOffsetBall(angle, ShapeData.COS);
				setX(displacement);
				displacement = getZ()
						+ ShapeData.angleOffsetBall(angle, ShapeData.SIN);
				setZ(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Z) {
				displacement = getX()
						+ ShapeData.angleOffsetBall(angularOffset, ShapeData.SIN);
				setX(displacement);
				displacement = getY()
						- ShapeData.angleOffsetBall(angularOffset, ShapeData.COS);
				setY(displacement);
			}
		}
		// PLACE ON FRONT ROD
		else if (rod.getPositionOfRod() == ShapeData.FRONT) {
			if (rod.getRotation() == ShapeData.ROTATE_X) {
				displacement = getY()
						- ShapeData.angleOffsetBall(angle, ShapeData.SIN);
				setY(displacement);
				displacement = getZ()
						+ ShapeData.angleOffsetBall(angle, ShapeData.COS);
				setZ(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Y) {
				displacement = getX()
						+ ShapeData.angleOffsetBall(angularOffset, ShapeData.COS);
				setX(displacement);
				displacement = getZ()
						- ShapeData.angleOffsetBall(angularOffset, ShapeData.SIN);
				setZ(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Z
					|| rod.getRotation() == ShapeData.ROTATE_DISABLE) {
				displacement = getZ() + ShapeData.C_OFFSET;
				setZ(displacement);
			}
		}
		// PLACE ON BACK ROD
		else if (rod.getPositionOfRod() == ShapeData.BACK) {
			if (rod.getRotation() == ShapeData.ROTATE_X) {
				displacement = getY()
						+ ShapeData.angleOffsetBall(angle, ShapeData.SIN);
				setY(displacement);
				displacement = getZ()
						- ShapeData.angleOffsetBall(angle, ShapeData.COS);
				setZ(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Y) {
				displacement = getX()
						- ShapeData.angleOffsetBall(angularOffset, ShapeData.COS);
				setX(displacement);
				displacement = getZ()
						+ ShapeData.angleOffsetBall(angularOffset, ShapeData.SIN);
				setZ(displacement);
			}
			if (rod.getRotation() == ShapeData.ROTATE_Z
					|| rod.getRotation() == ShapeData.ROTATE_DISABLE) {
				displacement = getZ() - ShapeData.C_OFFSET;
				setZ(displacement);
			}
		}
	}

}
